﻿Public Class FrmSecciones
    Dim classController As New Class_RC_Controller.rc_Depositos
    Private _codSeccion As Integer = 0

    Public Property codSeccion() As Integer
        Get
            Return _codSeccion
        End Get
        Set(ByVal value As Integer)
            _codSeccion = value
        End Set
    End Property
    
    Private Sub FrmSecciones_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CargarDatos()
        Me.llenardeposito()
    End Sub
    Private Sub CargarDatos()
        Try
            If codSeccion > 0 Then

                Dim ds As New DataTable
                ds = classController.get_Secciones_All(oiml.dbConn, codSeccion, "", 0)

                If Not ds Is Nothing Then
                    txtcodigo.Text = ds.Rows(0)("codSeccion")
                    txtdescripcion.Text = ds.Rows(0)("descripcionSeccion")
                    txtobseccion.Text = ds.Rows(0)("ObsSeccion")
                    btneliminar.Enabled = True
                End If
            Else
                btneliminar.Enabled = False
                txtdescripcion.Focus()
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub
    Private Sub llenardeposito()
        Dim dsdeposito As New DataTable
        dsdeposito = classController.get_Depositos_All(oiml.dbConn, 0, "")
        If Not dsdeposito Is Nothing Then
            If dsdeposito.Rows.Count > 0 Then
                cmbdeposito.DataSource = dsdeposito
                cmbdeposito.DisplayMember = "descripcionDep"
                cmbdeposito.ValueMember = "codDeposito"
            End If
        End If
    End Sub
    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtDescripcion.Text = "" Then
            ErrorProvider.SetError(txtDescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtDescripcion, "")
        End If        '

        Return controlCampos

    End Function


    Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click
        If ValidarCampos() Then
            Exit Sub
        End If

        If codSeccion = 0 Then
            codSeccion = classController.Insert_Seccion(oiml.dbConn, txtdescripcion.Text, cmbdeposito.SelectedValue, txtobseccion.Text)
            If codSeccion > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Seccion", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Seccion", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_Seccion(oiml.dbConn, codSeccion, txtdescripcion.Text, cmbdeposito.SelectedValue, txtobseccion.Text) Then
                MessageBox.Show("Registro grabado con exito!", "Seccion", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Seccion", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If
    End Sub

    Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancelar.Click
        Me.Close()
    End Sub



    Private Sub btneliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneliminar.Click
        If codSeccion > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Seccion", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_Seccion(oiml.dbConn, codSeccion) Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Seccion", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If
    End Sub
End Class